package com.net.shop.car.manager.ui.personalcenter.settings;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.net.shop.car.manager.ActivityCollector;
import com.net.shop.car.manager.Constant;
import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.volley.VolleyCenter;
import com.net.shop.car.manager.api.volley.VolleyListenerImpl;
import com.net.shop.car.manager.api.volley.request.Login;
import com.net.shop.car.manager.api.volley.request.ModifyPassword;
import com.net.shop.car.manager.api.volley.response.LoginResponse;
import com.net.shop.car.manager.api.volley.response.ModifyPasswordResponse;
import com.net.shop.car.manager.api.volley.response.RegisterResponse;
import com.net.shop.car.manager.ui.FullScreenDialog;
import com.net.shop.car.manager.ui.MainActivity;
import com.net.shop.car.manager.utils.Constants;

public class ReSetPsdDialog extends FullScreenDialog {
	private ImageView title_back_btn;
	private TextView title_txt;
	private EditText old_psd;
	private EditText new_psd;
	private EditText new_psd_sure;
	private Button sure_resetpsd;

	public ReSetPsdDialog(Context context) {
		super(context, R.style.Dialog_Fullscreen);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_resetpsd);
		title_back_btn = (ImageView) findViewById(R.id.title_back_btn);
		title_txt = (TextView) findViewById(R.id.title_txt);
		title_back_btn.setOnClickListener(onClickListener);
		title_txt.setText("修改密码");
		old_psd = (EditText) findViewById(R.id.old_psd);
		new_psd_sure = (EditText) findViewById(R.id.new_psd_sure);
		new_psd = (EditText) findViewById(R.id.new_psd);
		sure_resetpsd = (Button) findViewById(R.id.sure_resetpsd);
		sure_resetpsd.setOnClickListener(onClickListener);
	}

	private android.view.View.OnClickListener onClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.sure_resetpsd:
				doReSetPsd();
				break;
			case R.id.title_back_btn:
				dismiss();
				break;

			default:
				break;
			}

		}

	};

	protected void doReSetPsd() {
		// TODO Auto-generated method stub
		String oldPsd = old_psd.getText().toString();
		if (oldPsd.length() < 6 || oldPsd.length() > 16) {
			Toast.makeText(activity, "密码必须是6至16位的有效字符", Toast.LENGTH_LONG).show();
			old_psd.requestFocus();
			return;
		}
		String strnewpsd = new_psd.getText().toString();
		if (strnewpsd.length() < 6 || strnewpsd.length() > 16) {
			Toast.makeText(activity, "新密码必须是6至16位的有效字符", Toast.LENGTH_LONG).show();
			old_psd.requestFocus();
			return;
		}
		String strnewpsdsure = new_psd.getText().toString();
		if (!strnewpsdsure.equals(strnewpsd)) {
			Toast.makeText(activity, "新密码与验证密码不一致", Toast.LENGTH_LONG).show();
			old_psd.requestFocus();
			return;
		}
		ModifyPassword mdp = new ModifyPassword(activity.getStringFromSharedPreferences(Constant.sp.login_name), oldPsd, strnewpsd);
		VolleyCenter.getVolley().addPostRequest(mdp, new VolleyListenerImpl<ModifyPasswordResponse>(new ModifyPasswordResponse()) {

			@Override
			public void deleveryResponse(ModifyPasswordResponse response) {
				if (response.isSuccess()) {
					// actionStart(MainActivity.class);
					// saveToSharedPreferences(Constant.sp.user_id,
					// response.getMemberId());
					// ActivityCollector.finishActivity("RegisterActivity");
					// finish();
					Toast.makeText(activity, "密码修改成功，请牢记", Toast.LENGTH_LONG).show();
					dismiss();
				} else {
					Toast.makeText(activity, response.getErrorMsg(), Toast.LENGTH_LONG).show();
				}
			}
		});
	}
}
